﻿Imports Trinkets.Core

Public Class ListWindow
    Inherits BaseWindowHandler

    Private _dataGrid As DataGrid = Nothing

    Private Sub New(ByVal table As TableSchema, ByVal database As CsvDatabase, ByVal title As String)
        MyBase.New(table, database, title, 1000, 700)
    End Sub

    Friend Shared Function Generate(ByVal database As CsvDatabase, ByVal table As TableSchema) As ListWindow

        ' Create a new window
        Dim Result As New ListWindow(table, database, "List of " + table.MultipleCaption)

        ' Create the button to close the window
        Dim CloseButton As Button = ControlGenerator.GenerateButton("Close", 75, 23, New Thickness(0, 0, 12, 12))
        CloseButton.HorizontalAlignment = Windows.HorizontalAlignment.Right
        CloseButton.VerticalAlignment = Windows.VerticalAlignment.Bottom
        Result._grid.Children.Add(CloseButton)
        AddHandler CloseButton.Click, AddressOf Result.HandleCloseButtonClicked

        ' Create the button to insert a new record
        Dim InsertButton As Button = ControlGenerator.GenerateButton("Add", 75, 23, New Thickness(0, 0, 99, 12))
        InsertButton.HorizontalAlignment = Windows.HorizontalAlignment.Right
        InsertButton.VerticalAlignment = Windows.VerticalAlignment.Bottom
        Result._grid.Children.Add(InsertButton)
        AddHandler InsertButton.Click, AddressOf Result.HandleInsertButtonClicked

        ' Create the button to edit the selected record
        Dim EditButton As Button = ControlGenerator.GenerateButton("Edit", 75, 23, New Thickness(0, 0, 186, 12))
        EditButton.HorizontalAlignment = Windows.HorizontalAlignment.Right
        EditButton.VerticalAlignment = Windows.VerticalAlignment.Bottom
        Result._grid.Children.Add(EditButton)
        AddHandler EditButton.Click, AddressOf Result.HandleEditButtonClicked

        ' Create the button to delete the selected record
        Dim DeleteButton As Button = ControlGenerator.GenerateButton("Delete", 75, 23, New Thickness(0, 0, 273, 12))
        DeleteButton.HorizontalAlignment = Windows.HorizontalAlignment.Right
        DeleteButton.VerticalAlignment = Windows.VerticalAlignment.Bottom
        Result._grid.Children.Add(DeleteButton)
        AddHandler DeleteButton.Click, AddressOf Result.HandleDeleteButtonClicked

        ' Populate a data grid
        Result._dataGrid = New DataGrid
        DataGridManager.Update(Result._dataGrid, database, table)
        Result._dataGrid.Margin = New Thickness(12, 12, 12, 41)
        Result._grid.Children.Add(Result._dataGrid)

        ' Wire up double click events on the grid
        AddHandler Result._dataGrid.MouseDoubleClick, AddressOf Result.HandleGridDoubleClick

        ' Return the window
        Return Result

    End Function

    Private Sub HandleCloseButtonClicked(ByVal sender As Object, ByVal e As RoutedEventArgs)
        _window.DialogResult = True
    End Sub

    Private Sub HandleInsertButtonClicked(ByVal sender As Object, ByVal e As RoutedEventArgs)

        Dim Form As EditWindow = EditWindow.Generate(_database, _table)
        If Form.ShowDialog = True Then DataGridManager.Update(_dataGrid, _database, _table)

    End Sub

    Private Sub HandleEditButtonClicked(ByVal sender As Object, ByVal e As RoutedEventArgs)

        ' Ensure a record has been selected
        If _dataGrid.SelectedItem Is Nothing Then
            MsgBox("Please select a " + _table.SingularCaption + " to edit.", MsgBoxStyle.Exclamation)
            Exit Sub
        End If

        ' Generate and show the form
        Dim ExistingEntity As BaseEntity = _dataGrid.SelectedItem
        Dim Form As EditWindow = EditWindow.Generate(_database, _table, ExistingEntity)
        If Form.ShowDialog = True Then DataGridManager.Update(_dataGrid, _database, _table)

    End Sub

    Private Sub HandleDeleteButtonClicked(ByVal sender As Object, ByVal e As RoutedEventArgs)

        ' Ensure a record has been selected
        If _dataGrid.SelectedItem Is Nothing Then
            MsgBox("Please select a " + _table.SingularCaption + " to delete.", MsgBoxStyle.Exclamation)
            Exit Sub
        End If

        ' Confirm that the user actually wants to delete the record
        If MsgBox("Are you sure you wish to delete the selected " + _table.SingularCaption + "?", MsgBoxStyle.Question + MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            Dim ExistingEntity As BaseEntity = _dataGrid.SelectedItem
            _database.Tables(_table.Name).DeleteRecord(ExistingEntity)
            DataGridManager.Update(_dataGrid, _database, _table)
        End If

    End Sub

    Private Sub HandleGridDoubleClick(ByVal sender As Object, ByVal e As Input.MouseButtonEventArgs)

        ' Ensure a record has been selected
        If _dataGrid.SelectedItem IsNot Nothing Then

            ' Generate and show the form
            Dim ExistingEntity As BaseEntity = _dataGrid.SelectedItem
            Dim Form As EditWindow = EditWindow.Generate(_database, _table, ExistingEntity)
            If Form.ShowDialog = True Then DataGridManager.Update(_dataGrid, _database, _table)

        End If

    End Sub

End Class
